-- Unity XLua 桥接层
-- 负责Lua系统与Unity C#之间的通信

local LuaBridge = {}
local Logger = require("utils.logger")

-- Unity回调函数注册表
local unity_callbacks = {
    on_battle_start = nil,
    on_turn_start = nil,
    on_action_execute = nil,
    on_character_damage = nil,
    on_character_heal = nil,
    on_battle_end = nil,
    on_skill_effect = nil,
    on_status_effect = nil
}

-- 注册Unity回调函数
function LuaBridge.register_callback(event_name, callback_func)
    if unity_callbacks[event_name] then
        unity_callbacks[event_name] = callback_func
        Logger.info("Unity回调注册成功: " .. event_name)
    else
        Logger.error("未知的事件名称: " .. event_name)
    end
end

-- 触发Unity回调
function LuaBridge.trigger_unity_callback(event_name, ...)
    local callback = unity_callbacks[event_name]
    if callback then
        local success, result = pcall(callback, ...)
        if not success then
            Logger.error("Unity回调执行失败: " .. event_name .. ", 错误: " .. tostring(result))
        end
    else
        Logger.warn("未注册的Unity回调: " .. event_name)
    end
end

-- 战斗状态数据封装
function LuaBridge.create_battle_state_data(battle_state)
    return {
        battle_id = battle_state.battle_id,
        current_turn = battle_state.current_turn,
        battle_state = battle_state.battle_state,
        teams = {
            [1] = LuaBridge.create_team_data(battle_state.teams[1]),
            [2] = LuaBridge.create_team_data(battle_state.teams[2])
        },
        current_character = battle_state:get_current_character() and LuaBridge.create_character_data(battle_state:get_current_character()) or nil,
        turn_order = LuaBridge.create_turn_order_data(battle_state.turn_order, battle_state.characters)
    }
end

-- 队伍数据封装
function LuaBridge.create_team_data(team)
    local characters_data = {}
    for _, character in ipairs(team.characters) do
        table.insert(characters_data, LuaBridge.create_character_data(character))
    end
    
    return {
        name = team.name,
        characters = characters_data,
        score = team.score
    }
end

-- 角色数据封装
function LuaBridge.create_character_data(character)
    local current_stats = character:get_current_stats()
    
    return {
        id = character.id,
        name = character.name,
        level = character.level,
        team_id = character.team_id,
        position = character.position,
        is_alive = character.is_alive,
        is_controlled = character.is_controlled,
        
        -- 基础属性
        max_hp = character.max_hp,
        hp = character.hp,
        max_mp = character.max_mp,
        mp = character.mp,
        
        -- 当前属性（包含状态效果影响）
        current_stats = current_stats,
        
        -- 状态效果
        status_effects = LuaBridge.create_status_effects_data(character.status_effects),
        
        -- 技能列表
        skills = LuaBridge.create_skills_data(character.skills),
        
        -- 物品列表
        items = character.items
    }
end

-- 状态效果数据封装
function LuaBridge.create_status_effects_data(status_effects)
    local effects_data = {}
    for effect_name, effect in pairs(status_effects) do
        table.insert(effects_data, {
            name = effect_name,
            duration = effect.duration,
            stacks = effect.stacks or 1
        })
    end
    return effects_data
end

-- 技能数据封装
function LuaBridge.create_skills_data(skills)
    local skills_data = {}
    for _, skill in ipairs(skills) do
        table.insert(skills_data, {
            id = skill.id,
            name = skill.name,
            current_cooldown = skill.current_cooldown or 0,
            max_cooldown = skill.cooldown or 0,
            cost_mp = skill.cost_mp or 0,
            is_usable = skill:is_usable and skill:is_usable(nil) or false
        })
    end
    return skills_data
end

-- 行动顺序数据封装
function LuaBridge.create_turn_order_data(turn_order, characters)
    local order_data = {}
    for _, character_id in ipairs(turn_order) do
        for _, character in ipairs(characters) do
            if character.id == character_id then
                table.insert(order_data, {
                    character_id = character.id,
                    character_name = character.name,
                    speed = character.speed
                })
                break
            end
        end
    end
    return order_data
end

-- 决策数据封装
function LuaBridge.create_decision_data(decision)
    local decision_data = {
        type = decision.type,
        score = decision.score,
        priority = decision.priority
    }
    
    if decision.type == "skill" then
        decision_data.skill_id = decision.skill.id
        decision_data.skill_name = decision.skill.name
        decision_data.target_id = decision.target.id
        decision_data.target_name = decision.target.name
    elseif decision.type == "item" then
        decision_data.item_id = decision.item.id
        decision_data.item_name = decision.item.name
        decision_data.target_id = decision.target.id
        decision_data.target_name = decision.target.name
    end
    
    return decision_data
end

-- 战斗结果数据封装
function LuaBridge.create_battle_result_data(result)
    return {
        winner_team_id = result.winner_team_id,
        reason = result.reason,
        rewards = result.rewards
    }
end

-- 导出模块
return LuaBridge